<?php

//No direct acesss
defined('_JEXEC') or die();

jimport('joomla.application.component.model');
jimport('joomla.client.helper');

class SimplelistingModelAuthors extends JModel {
        var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_table_prefix = null;

    function __construct()
    {
        parent::__construct();

        global $mainframe, $context;
        $this->_table_prefix = '#__simplelisting';
        $params =&$mainframe->getParams();

        $limit	= $mainframe->getUserStateFromRequest( $context.'limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }

    function getData()
    {
        if (empty($this->_data))
        {
                $query = $this->_buildQuery();
                $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->_data;
    }

    function getTotal()
    {
        if (empty($this->_total))
        {
                $query = $this->_buildQuery();
                $this->_total = $this->_getListCount($query);
        }

        return $this->_total;
    }

    function getPagination()
    {
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }

    function _buildQuery()
    {
        $where = $this->_buildContentWhere();
        $orderby = $this->_buildContentOrderBy();

        $query = 'SELECT a.*,  cd.country as `usercountry`
        FROM #__users AS a
        LEFT JOIN #__contact_details AS cd ON cd.user_id = a.id'.
        $where
        .$orderby;
        return $query;

    }

    function _buildContentWhere()
    {
       global $mainframe;

//        $where = ' WHERE s.id IS null';

        return $where;
    }

    function _buildContentOrderBy()
    {
        global $mainframe, $context;


        $orderby = ' ORDER BY a.name DESC';

        return $orderby;
    }
            
//    function getAuthors($authors)
//            {
//            $db = & JFactory::getDBO();
//            $query = 'SELECT COUNT(*) FROM #__simplelisting AS a'.
//            ' WHERE a.id='.$author;
//            $db->setQuery($query);
//            $result = $db->loadResult();
//            return $result;
//            }

}
?>
